import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.*;

public class MazeGameUI implements KeyListener
{
	static JFrame frame = new JFrame();
	static GameState maze = null;
	static GraphicsJPanel mazePanel = null;
	
	public static void main (String[] args)
	{
		JPanel mainMenu = new JPanel();
		mainMenu.setLayout(new GridLayout(4,1));
		
		JButton start = new JButton("Start!");
		JButton options = new JButton("Options");
		JButton scores = new JButton("Scores");
		JButton exit = new JButton("Exit");
		
		ActionListener click = new MouseClickListener("start");
		start.addActionListener(click);
		click = new MouseClickListener("options");
		options.addActionListener(click);
		click = new MouseClickListener("scores");
		scores.addActionListener(click);
		click = new MouseClickListener("exit");
		exit.addActionListener(click);
		
		mainMenu.add(start);
		mainMenu.add(options);
		mainMenu.add(scores);
		mainMenu.add(exit);
		
		frame.add(mainMenu);
		
	    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    frame.setVisible( true );
	    frame.setSize( 250 , 300 );
	    frame.setResizable(false);
	    frame.setTitle( "Get Lost!" );
	}
	
	public static void startMenu()
	{
		JPanel startMenu = new JPanel();
		startMenu.setLayout(new GridLayout(4,1));
		JButton smallMaze = new JButton("Small Maze");
		JButton mediumMaze = new JButton("Medium Maze");
		JButton largeMaze = new JButton("Large Maze");
		JButton back = new JButton("Back");
		
		ActionListener click = new MouseClickListener("smallMaze");
		smallMaze.addActionListener(click);
		click = new MouseClickListener("mediumMaze");
		mediumMaze.addActionListener(click);
		click = new MouseClickListener("largeMaze");
		largeMaze.addActionListener(click);
		click = new MouseClickListener("introMenu");
		back.addActionListener(click);
		
		startMenu.add(smallMaze);
		startMenu.add(mediumMaze);
		startMenu.add(largeMaze);
		startMenu.add(back);
		
		frame.getContentPane().removeAll();
		frame.add(startMenu);
		
		frame.validate();
		frame.repaint();
	}
	
	public static void smallMaze()
	{
		maze = new GameState(15, 15);
		ArrayList<String> s = maze.printGame();
		JFrame smallMazeFrame = new JFrame();
		
		mazePanel = new GraphicsJPanel(s);
		
		MazeGameUI mgu = new MazeGameUI();
		smallMazeFrame.addKeyListener(mgu);
		
		smallMazeFrame.add(mazePanel);
		
		smallMazeFrame.setSize(240,260);
        smallMazeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        smallMazeFrame.setVisible(true);
		smallMazeFrame.repaint();
        
		JPanel smallMazePanel = new JPanel();
		JLabel temporary = new JLabel("Small maze will eventually go here.");
		smallMazePanel.add(temporary);
		
		frame.getContentPane().removeAll();
		frame.add(smallMazePanel);
		
		frame.validate();
		frame.repaint();
	}
	
	public static void mediumMaze()
	{
		maze = new GameState(31, 31);
		ArrayList<String> s = maze.printGame();
		JFrame mediumMazeFrame = new JFrame();
		
		GraphicsJPanel g = new GraphicsJPanel(s);
		mediumMazeFrame.add(g);
		
		mazePanel = new GraphicsJPanel(s);
		
		MazeGameUI mgu = new MazeGameUI();
		mediumMazeFrame.addKeyListener(mgu);
		
		mediumMazeFrame.add(mazePanel);
		
		mediumMazeFrame.setSize(445,465);
        mediumMazeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mediumMazeFrame.setVisible(true);
		mediumMazeFrame.repaint();
		
		JPanel mediumMazePanel = new JPanel();
		JLabel temporary = new JLabel("Medium maze will eventually go here.");
		mediumMazePanel.add(temporary);
		
		frame.getContentPane().removeAll();
		frame.add(mediumMazePanel);
		
		frame.validate();
		frame.repaint();
	}
	
	public static void largeMaze()
	{
		maze = new GameState(31, 31);
		ArrayList<String> s = maze.printGame();
		JFrame largeMazeFrame = new JFrame();
		
		GraphicsJPanel g = new GraphicsJPanel(s);
		largeMazeFrame.add(g);
		
		mazePanel = new GraphicsJPanel(s);
		
		MazeGameUI mgu = new MazeGameUI();
		largeMazeFrame.addKeyListener(mgu);
		
		largeMazeFrame.add(mazePanel);
		
		largeMazeFrame.setSize(445,465);
        largeMazeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        largeMazeFrame.setVisible(true);
		largeMazeFrame.repaint();
		
		JPanel largeMazePanel = new JPanel();
		JLabel temporary = new JLabel("Large maze will eventually go here.");
		largeMazePanel.add(temporary);
		
		frame.getContentPane().removeAll();
		frame.add(largeMazePanel);
		
		frame.validate();
		frame.repaint();
	}
	
	public static void introMenu()
	{
		JPanel mainMenu = new JPanel();
		mainMenu.setLayout(new GridLayout(4,1));
		
		JButton start = new JButton("Start!");
		JButton options = new JButton("Options");
		JButton scores = new JButton("Scores");
		JButton exit = new JButton("Exit");
		
		ActionListener click = new MouseClickListener("start");
		start.addActionListener(click);
		click = new MouseClickListener("options");
		options.addActionListener(click);
		click = new MouseClickListener("scores");
		scores.addActionListener(click);
		click = new MouseClickListener("exit");
		exit.addActionListener(click);
		
		mainMenu.add(start);
		mainMenu.add(options);
		mainMenu.add(scores);
		mainMenu.add(exit);
		
		frame.getContentPane().removeAll();
		frame.add(mainMenu);
		
		frame.validate();
		frame.repaint();
	}
	
	public static void options()
	{
		JPanel colorSliders = new JPanel();
		JPanel optionsMenu = new JPanel();
		optionsMenu.setLayout(new GridLayout(2,1));
		
		JLabel temporary = new JLabel("Color sliders will eventually go here.");
		colorSliders.add(temporary);
		
		JButton resetScores = new JButton("Reset Scores");
		JButton back = new JButton("Back");
		
		ActionListener click = new MouseClickListener("resetScores");
		resetScores.addActionListener(click);
		click = new MouseClickListener("introMenu");
		back.addActionListener(click);
		
		optionsMenu.add(resetScores);
		optionsMenu.add(back);
		
		frame.getContentPane().removeAll();
		frame.setLayout(new GridLayout(2,1));
		frame.add(colorSliders);
		frame.add(optionsMenu);
		
		frame.validate();
		frame.repaint();
		frame.setLayout(new GridLayout());
	}
	
	public static void scoreMenu()
	{
		JPanel scores = new JPanel();
		
		JPanel buttons = new JPanel();
		JButton smallMazeScores = new JButton("Small");
		JButton mediumMazeScores = new JButton("Medium");
		JButton largeMazeScores = new JButton("Large");
		buttons.add(smallMazeScores);
		buttons.add(mediumMazeScores);
		buttons.add(largeMazeScores);
		
		JLabel temporary = new JLabel("Scores will eventually go here.");
		JPanel blankPanel = new JPanel();
		JPanel backButton = new JPanel();
		
		JButton back = new JButton("Back");
		ActionListener click = new MouseClickListener("introMenu");
		back.addActionListener(click);
		backButton.add(back);
		
		
		scores.setLayout(new GridLayout(4,1));
		scores.add(buttons);
		scores.add(temporary);
		scores.add(blankPanel);
		scores.add(backButton);
		
		frame.getContentPane().removeAll();
		frame.add(scores);
		
		frame.validate();
		frame.repaint();
	}
	
	public static void resetScores()
	{
		JPanel areYouSure = new JPanel();
		JLabel prompt = new JLabel("Are you sure?");
		areYouSure.add(prompt);
		
		JButton yes = new JButton("Yes");
		JButton no = new JButton("No");
		
		ActionListener click = new MouseClickListener("options");
		yes.addActionListener(click);
		no.addActionListener(click);
		
		frame.getContentPane().removeAll();
		frame.setLayout(new GridLayout(3,1));
		frame.add(areYouSure);
		frame.add(yes);
		frame.add(no);
		
		frame.validate();
		frame.repaint();
		
		frame.setSize(250,300);
		frame.setLayout(new GridLayout());
	}
	
	public static void close()
	{
		frame.setVisible(false);
		System.exit(0);
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		if (e.getKeyCode() == 37)
			maze.movePlayer(0, -1);
		if (e.getKeyCode() == 38)
			maze.movePlayer(-1, 0);
		if (e.getKeyCode() == 39)
			maze.movePlayer(0, 1);
		if (e.getKeyCode() == 40)
			maze.movePlayer(1, 0);
		
		if ( maze.playerAtExit() )
		{
			JFrame youWin = new JFrame();
			JLabel label = new JLabel("Yay! You won!");
			label.setHorizontalAlignment(SwingConstants.CENTER);
			youWin.add(label);
		
		    youWin.setVisible( true );
		    youWin.setSize( 200 , 100 );
		    youWin.setResizable(false);
		}
		
		mazePanel.setMaze(maze.printGame());
		mazePanel.repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		
	}

	@Override
	public void keyTyped(KeyEvent e) { }
}